package com.weixin.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;

import com.jfinal.plugin.activerecord.Db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.weixin.dao.Use;
import com.weixin.utils.DateUtils;

import freemarker.template.utility.DateUtil;

public class UserController extends Controller{
	private Use dao = new Use();
	private Date date = new Date();
	public void index() {
		// TODO Auto-generated method stub
	}
	
	public void toDetail(){
		Db.update("update admin set num = num+1");
		String provinces = getPara("provinces");
		String city = getPara("city");
		String stores = getPara("stores");
		
		
		String province_name = getPara("province_name");
		String city_name = getPara("city_name");
		String store_name = getPara("store_name");
		
		try {
			province_name   = new String( province_name.getBytes("ISO-8859-1") , "utf-8");
			city_name   = new String( city_name.getBytes("ISO-8859-1") , "utf-8");
			store_name   = new String( store_name.getBytes("ISO-8859-1") , "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setAttr("province_name", province_name);
		setAttr("city_name", city_name);
		setAttr("store_name", store_name);
		
		setAttr("province_id", provinces);
		setAttr("city_id", city);
		setAttr("store_id", stores);
		renderJsp("/static/detail.jsp");
	}
	
	public void save(){
		Db.update("update admin set num = num+1");
		String city_id = getPara("city_id");
		String store_id = getPara("store_id");
		String gamenum=getPara("gamenum");
		String provinces = getPara("provinces");
		String name = getPara("name");
		String phone = getPara("phone");
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		if (phone == null) {
			renderJson("code", 0);
			return;
		}
		Use user = new Use();
		user.set("city_id",city_id );
		user.set("store_id",store_id );
		user.set("gamenum",gamenum);
		user.set("name",name );
		user.set("phone",phone );
		user.set("date",date );
		Use user1 = new Use();
		String sql = "select DISTINCT * from users where phone = "+ phone;
		user1 = dao.findFirst(sql);
		int game1;
		if(user1 != null){
				game1 = user1.getInt("gamenum");
				int game2 = Integer.parseInt(gamenum);
				if (game2>game1) {
					user1.set("gamenum", game2);
					user1.set("date", date);
					user1.update();
			}
		}else{
			dao.save(user);
		}
		setAttr("city", city_id);
		setAttr("provinces", provinces);
		setAttr("stores", store_id);
		getTop(phone);
	}


	/**
	 * 输入手机号查询排名
	 */
	public void getTopAndMe(){
		String phone = getPara("phone");
		String provinces1 =getPara("provinces1");
		String stores1 = getPara("stores1");
		String citys1 = getPara("city1");
		
		String province1_name = getPara("province1_name");
		String store1_name = getPara("store1_name");
		String city1_name = getPara("city1_name");
		
		try {
			province1_name   = new String( province1_name.getBytes("ISO-8859-1") , "utf-8");
			store1_name   = new String( store1_name.getBytes("ISO-8859-1") , "utf-8");
			city1_name   = new String( city1_name.getBytes("ISO-8859-1") , "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		List<Use> users = dao.find("select * from users "
				+ "where is_get_persent = false "
				+ "order by gamenum DESC limit ?",20);
		List<Use> user = dao.find("select * from users order by gamenum desc");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<Use> list = new ArrayList<Use>();
		for(int j=0;j<users.size();j++){
			Use user1 = users.get(j);
			String timeStr = user1.getDate("date").toString();
			String date = sdf.format(user1.getDate("date"));
			user1.set("date", date);
			user1.set("time", timeStr.substring(11,timeStr.length()-2));
			list.add(user1);
		}
		int rank = 0;
		for (int i = 0; i < user.size(); i++) {
			if (user.get(i).get("phone").equals(phone)) {
				rank = i+1;
				break;
			}
		}
		setAttr("rank", rank);
		setAttr("list", list);
		setAttr("provinces", provinces1);
		setAttr("stores", stores1);
		setAttr("city", citys1);
		
		setAttr("province_name", province1_name);
		setAttr("store_name", store1_name);
		setAttr("city_name", city1_name);
		
		renderJsp("/static/leaderboard.jsp");
	}


	/**
	 * 输入手机号查询排名
	 */
	public void getTop(String phone){
		List<Use> users = dao.find("select * from users "
				+ "where is_get_persent = false "
				+ "order by gamenum DESC limit 20");
		List<Use> user = dao.find("select * from users order by gamenum desc");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<Use> list = new ArrayList<Use>();
		for(int j=0;j<users.size();j++){
			Use user1 = users.get(j);
			String timeStr = "";
			try {
				timeStr = user1.getDate("date").toString();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			String date = "";
			try {
				date = sdf.format(user1.getDate("date"));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			user1.set("date", date);
			user1.set("time", timeStr.substring(11,timeStr.length()-2));
			list.add(user1);
		}
		int rank = 0;
		for (int i = 0; i < user.size(); i++) {
			if (user.get(i).get("phone").equals(phone)) {
				rank = i+1;
				break;
			}
		}
		setAttr("rank", rank);
		setAttr("list", list);
		renderJsp("/static/leaderboard.jsp");
	}


}
